package editorsvg.editor.documento;

import editorsvg.editor.formas.Forma;
import editorsvg.editor.formas.Linea;
import editorsvg.editor.formas.Polilinea;
import java.awt.AlphaComposite;
import java.awt.Composite;
import java.awt.Graphics2D;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Area;
import java.awt.geom.Rectangle2D;

/**
 *
 * @author Jorge Berjano
 */
public class ElementoForma extends ElementoGrafico {

    private Forma forma;

    public ElementoForma(Forma forma) {
        assert (forma != null);
        this.forma = forma;
    }

    @Override
    protected void poblarPropiedades() {
        super.poblarPropiedades();
        agregarPropiedades(forma.getPropiedades());
        
    }

    @Override
    protected void asignarPropietariosPropiedades() {
        super.asignarPropietariosPropiedades();
        forma.getPropiedades().setPropietario(forma);        
    }

    @Override
    public String getNombreTag() {
        return forma.getNombreTag();
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        ElementoForma clon = (ElementoForma) super.clone();
        clon.forma = (Forma) forma.clone();
        return clon;
    }

    @Override
    public void actualizar() {
        getForma().actualizar();
    }

    @Override
    public Rectangle2D getEncuadre() {
        if (forma instanceof Linea || forma instanceof Polilinea) {
            return getShapeTransformado().getBounds2D();
        }

        Shape shape = getShapeTransformado();
        Area area = new Area(shape);
        return area.getBounds2D();
    }

    public Shape getShapeTransformado() {
        Shape s = getForma().getShape();
        return transformacion().transformar(s);
    }

    @Override
    public Shape getContorno() {
        return getShapeTransformado();
    }

    @Override
    public void pintar(Graphics2D graphics, AjustesGraficos ajustes) {
        super.pintar(graphics, ajustes);

        Paint paintInicial = graphics.getPaint();
        Composite compositeInicial = graphics.getComposite();        
        AffineTransform transfomacionInicial = graphics.getTransform();
        
        if (getTransformacion() != null) {
            graphics.transform(getTransformacion());
        }
        Shape shape = getForma().getShape();
        assert (shape != null);

        if (ajustes.getPinturaRelleno() != null) {
            graphics.setPaint(ajustes.getPinturaRelleno());
            if (ajustes.getOpacidadRelleno() != null) {
                graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, ajustes.getOpacidadRelleno().floatValue()));
            }
            graphics.fill(shape);
        }
        if (ajustes.getPinturaTrazo() != null && ajustes.getTrazo() != null) {
            graphics.setPaint(ajustes.getPinturaTrazo());
            graphics.setStroke(ajustes.getTrazo());
            if (ajustes.getOpacidadTrazo() != null) {
                graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, ajustes.getOpacidadTrazo().floatValue()));
            }
            graphics.draw(shape);
        }

        graphics.setPaint(paintInicial);
        graphics.setComposite(compositeInicial);
        graphics.setTransform(transfomacionInicial);
    }

    public Forma getForma() {
        return forma;
    }
}
